package com.sh.studytest.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sh.studytest.R;
import com.sh.studytest.adapter.SubscriptionAdapter;
import com.sh.studytest.bean.SubscriptionOption;

import java.util.ArrayList;
import java.util.List;


public class CustomDialogFragment extends DialogFragment {

    private RecyclerView recyclerView;
    private SubscriptionAdapter adapter;
    private List<SubscriptionOption> subscriptionOptions;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // 使用自定义布局
        View view = LayoutInflater.from(getContext()).inflate(R.layout.custom_pay, null);

        // 设置RecyclerView
        recyclerView = view.findViewById(R.id.recycler_view);
//        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        // 创建订阅选项数据
        subscriptionOptions = new ArrayList<>();
        subscriptionOptions.add(new SubscriptionOption("1个月", 22, 0.73));
        subscriptionOptions.add(new SubscriptionOption("3个月", 6, 2.0));
        subscriptionOptions.add(new SubscriptionOption("12个月", 2300, 268));
        subscriptionOptions.add(new SubscriptionOption("12个月", 2300, 268));
        subscriptionOptions.add(new SubscriptionOption("12个月", 2300, 268));
        subscriptionOptions.add(new SubscriptionOption("12个月", 2300, 268));



        // 设置适配器
        adapter = new SubscriptionAdapter(getContext(), subscriptionOptions);
        recyclerView.setAdapter(adapter);


        // 创建 Dialog
        Dialog dialog = new Dialog(getContext());
        dialog.setContentView(view);
        return dialog;
    }


    // 改变 CustomDialogFragment 的大小
    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = (int) (getResources().getDisplayMetrics().widthPixels * 0.9); // 设置宽度为屏幕宽度的 80%
                lp.height = (int) (getResources().getDisplayMetrics().heightPixels * 0.7); // 设置高度为屏幕高度的 60%
                window.setAttributes(lp);
            }
        }
    }
}